package eteam.aps.controller;

import eteam.aps.common.R;
import eteam.aps.common.component.LoginUserRedisService;
import eteam.aps.common.exceptions.AuthException;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.model.entity.OnlyProduct;
import eteam.aps.model.entity.ProductParentEntity;
import eteam.aps.model.vo.OnlyProductVo;
import eteam.aps.model.vo.ProductInfoVo;
import eteam.aps.service.OnlyProductService;
import eteam.aps.service.ProductInfoService;
import eteam.aps.service.QueryInfoService;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 唯一产品控制层
 *
 * @author WMT
 * @Date 2023/2/10 11:54
 */
@Api(tags = "唯一产品模块")
@RestController
public class OnlyProductController implements IOnlyProductController {

    @Autowired
    private OnlyProductService onlyProductService;

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private LoginUserRedisService loginUserRedisService;

    @Override
    @ApiOperation("设置冻结状态")
    @PostMapping("/op/setFreeze/{fId}")
    public R<Integer> setFreeze(@PathVariable String fId) throws Exception, AuthException {
        VerifyTokenDto user = loginUserRedisService.getUser();
        OnlyProduct onlyProduct = new OnlyProduct();
        onlyProduct.setfId(fId);
        onlyProduct.setfState(ProductParentEntity.FSTATE_FREEZE);
        onlyProduct.setfFreezeNumber((int) ((Math.random() * 9 + 1) * 100000));
        onlyProduct.setfLastModifyUserId(user.getFuserid());
        onlyProduct.setfLastModifyTime(LocalDateTime.now());
        return R.success(onlyProductService.setFreeze(onlyProduct));
    }

    @Autowired
    private QueryInfoService queryInfoService;

    @Override
    @ApiOperation("分页查询唯一产品")
    @PostMapping("/op/selectAll")
    public R<JsonPage<OnlyProductVo>> selectAll(
            @RequestParam(value = "fProductCode", required = false) String fProductCode,
            @RequestParam(value = "fProductName", required = false) String fProductName,
            @RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize,
            @RequestParam("productCategory") Integer productCategory
    ) throws Exception, AuthException {
        Map<String, Object> map = new HashMap<>(5);
        map.put("productCategory",productCategory);
        if (Helper.isNotEmpty(fProductCode)){
            map.put("fProductCode",Helper.likeSql(fProductCode));
        }
        if (Helper.isNotEmpty(fProductName)){
            map.put("fProductName",Helper.likeSql(fProductName));
        }
        Map<String, String> modelName = queryInfoService.selectModelName();
        Map<String, String> unitName = queryInfoService.selectUnitName();
        Map<String, String> producerName = queryInfoService.selectProducerName();
        JsonPage<OnlyProductVo> jsonPage = onlyProductService.selectAll(map, Helper.valuePositive(page, 1),
                Helper.valuePositive(pageSize, 10), productCategory);
        List<OnlyProductVo> list = jsonPage.getList();
        for (OnlyProductVo productVo : list) {
            productVo.setfProductModelName(modelName.get(productVo.getfProductModel()));
            productVo.setfProductUnitName(unitName.get(productVo.getfProductUnit()));
            productVo.setfProductAffName(producerName.get(productVo.getfProductAffiliation()));
        }
        return R.success(jsonPage);
    }

    @Override
    @PostMapping("/op/equipList")
    public R<List<ProductInfoVo>> selectEquipList() throws Exception, AuthException {
        return R.success(productInfoService.selectEquipList());
    }


}
